import axios from 'axios';
import { Rule, ApiResponse } from '../types';

const API_BASE = '/api';

export const rulesApi = {
  // 获取所有规则
  getRules: async (): Promise<Rule[]> => {
    const response = await axios.get(`${API_BASE}/rules`);
    return response.data;
  },

  // 获取单个规则
  getRule: async (index: number): Promise<Rule> => {
    const response = await axios.get(`${API_BASE}/rules/${index}`);
    return response.data;
  },

  // 创建规则
  createRule: async (rule: Rule): Promise<ApiResponse> => {
    const response = await axios.post(`${API_BASE}/rules`, rule);
    return response.data;
  },

  // 更新规则
  updateRule: async (index: number, rule: Rule): Promise<ApiResponse> => {
    const response = await axios.put(`${API_BASE}/rules/${index}`, rule);
    return response.data;
  },

  // 删除规则
  deleteRule: async (index: number): Promise<ApiResponse> => {
    const response = await axios.delete(`${API_BASE}/rules/${index}`);
    return response.data;
  },

  // 搜索规则
  searchRules: async (query: string): Promise<Rule[]> => {
    const response = await axios.get(`${API_BASE}/search`, { params: { q: query } });
    return response.data;
  },
};
